package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.product.config.minio.AppMinioProperties;
import com.atguigu.gmall.product.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

/**
 * @author lfy
 * @Description
 * @create 2023-07-12 10:57
 *
 * 能不能把文件上传抽取成一个组件，别人要用直接引入
 *
 */
@Slf4j
@Service
public class FileUploadServiceImpl implements FileUploadService {


    @Autowired
    AppMinioProperties minioProperties;


    @Override
    public String uploadFile(MultipartFile file) {
        String endpoint = minioProperties.getEndpoint();
        String accessKey = minioProperties.getAccessKey();
        String secretKey = minioProperties.getSecretKey();
        String bucketName = minioProperties.getBucketName();

        try {
            //1、创建MinioClient 连接Minio服务器进行操作  //遵循开发规范之，不要使用魔法值
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(endpoint)
                            .credentials(accessKey, secretKey)
                            .build();

            //2、创建一个Bucket
            BucketExistsArgs bucket = BucketExistsArgs.builder()
                    .bucket(bucketName)
                    .build();
            //3、判断指定的bucket是否存在
            boolean found = minioClient.bucketExists(bucket);
            if (!found) {
                // 4、创建Bucket用的参数  //
                MakeBucketArgs makeBucket = MakeBucketArgs.builder().bucket(bucketName).build();
                // 5、创建Bucket
                minioClient.makeBucket(makeBucket);
            }

            // 2022/12/12
            String path = DateUtil.formatDateToPath(new Date());
            String fileName = path +"/" +UUID.randomUUID().toString()+"_"+file.getOriginalFilename();
            // 6、上传文件. putObjectArgs: 上传对象
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType(file.getContentType()) //不说内容类型，默认浏览器会下载
                    .object(fileName) //文件名带上路径格式
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .build();
            // 6.1、构造出上传的参数
            minioClient.putObject(putObjectArgs);

            // 7、上传成功
            return endpoint+"/"+bucketName+"/"+fileName;

        } catch (Exception e) {
            log.error("文件上传失败，原因：{}",e.getCause());
        }

        //返回访问路径
        //http://192.168.200.100:9000/mall/BodieLighthouse_EN-AU10541981640_1920x1080.jpg
        return endpoint+"/"+bucketName+"/R.png";
    }
}
